package com.gxa.xly2024.service.impl;

import com.gxa.xly2024.dto.UserDTO;
import com.gxa.xly2024.mapper.RoleMapper;
import com.gxa.xly2024.mapper.UserMapper;
import com.gxa.xly2024.pojo.Role;
import com.gxa.xly2024.pojo.User;
import com.gxa.xly2024.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl {
    @Resource
    private UserMapper userMapper;

    public Map<String, Object> selectUserById(Integer id) {
        User user = this.userMapper.selectUserById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询id成功");
        map.put("data", user);
        return map;
    }

    public Map<String, Object> selectUserByLimit(UserVO userVO) {
        int index = (userVO.getPage() - 1) * userVO.getLimit();
        UserDTO userDTO = new UserDTO();  // alt+enter自动补全
        // 快速copy属性到指定对象中
        BeanUtils.copyProperties(userVO, userDTO);   // 这里代表的是，把vo和dto相同属性的值，copy过去
        userDTO.setIndex(index);

        System.out.println("userVO.getRoleName()=" + userVO.getRoleName());
        System.out.println("userVO.getUsername()=" + userVO.getUsername());

        List<User> users = this.userMapper.selectUserByLimit(userDTO);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "分页查询成功");
        map.put("count", this.userMapper.selectCount(userDTO));   // 查询当前表总条数
        map.put("data", users);
        return map;
    }

    public Map<String, Object> insert(User user) {
        // MD5加密  这里操作的一次加密数据
        String password = user.getPassword();
        byte[] bytes = password.getBytes(StandardCharsets.UTF_8);
        password = DigestUtils.md5DigestAsHex(bytes);
        user.setPassword(password);

        this.userMapper.insert(user);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增用户成功");
        map.put("data", user);
        return map;
    }

    public Map<String, Object> updateById(User user) {
        this.userMapper.updateById(user);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新用户成功");
        map.put("data", user);
        return map;
    }

    public Map<String, Object> deleteById(Integer id) {
        this.userMapper.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除用户成功");
        map.put("id", id);
        return map;
    }

    public Map<String, Object> selectEcharts() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询所有用户的年龄列表成功");
        map.put("data", this.userMapper.selectEcharts());
        return map;
    }

    public Map<String, Object> login(String username, String password, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 555);
        map.put("msg", "用户名或者密码错误");

        User user = this.userMapper.login(username);
        if(user == null) {
            map.put("code", 556);
            map.put("msg", "用户名错误");
            return map;
        }

        if(password == null) {
            map.put("code", 558);
            map.put("msg", "请输入密码");
            return map;
        }

        byte[] bytes = password.getBytes(StandardCharsets.UTF_8);   // 可能会抛出空指针异常
        String s = DigestUtils.md5DigestAsHex(bytes);

        if(user.getPassword().equals(s)) {  //
            map.put("code", 200);
            map.put("msg", "登录成功");
            map.put("data", user);
            request.getSession().setAttribute("user", user);   // 把用户的登录信息存放到session中
        }
        else {
            map.put("code", 557);
            map.put("msg", "密码错误");
        }

        return map;
    }
}
